package kr.saeildamil.androidstudy_second28;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class DownImage extends Activity {
	DownloadManager mDm;
	long mId=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch28_downimage);
		
		mDm=(DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
	}
	
	public void mOnClick(View v){
		switch (v.getId()) {
		case R.id.ch28_queuebtn:
			Uri uri=Uri.parse("http://developer.android.com/images/home.jpg");
			DownloadManager.Request req=new DownloadManager.Request(uri);
			req.setTitle("테스트 다운로드");
			req.setDescription("이미지 파일을 다운로드 합니다");
			req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE);
			mId=mDm.enqueue(req);
			
			IntentFilter filter=new IntentFilter();
			filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
			registerReceiver(mDownComplete, filter);
			break;

		default:
			break;
		}
	}
	
	BroadcastReceiver mDownComplete=new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(context, "다운로드 완료", Toast.LENGTH_SHORT).show();
		}
	};
}
